package graduation.dao.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import graduation.dao.FilesDao;
import graduation.entity.Files;
import graduation.vo.PageVO;

@Repository
public class FilesDaoImpl extends HibernateDaoSupport implements FilesDao{
	@Autowired  
    public void setSessionFactoryOverride(SessionFactory sessionFactory)  
    {  
        super.setSessionFactory(sessionFactory);  
    }
	
	@Resource
	private SessionFactory sessionFactory;

	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	/**
	 * 上传文件
	 */
	@Override
	public void uploadFiles(Files files) {
		// TODO Auto-generated method stub
		this.getSession().save(files);
	}
	/**
	 * 删除
	 */
	@Override
	public void delFiles(String fileId) {
		// TODO Auto-generated method stub
		String hql = "delete Files f where f.id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setParameter(0, fileId);
		query.executeUpdate();
	}

	/**
	 * 根据id查询
	 */
	@Override
	public Files getFilesById(String fileId) {
		// TODO Auto-generated method stub
		String hql = "from Files f where f.id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setParameter(0, fileId);
		return (Files) query.uniqueResult();
	}
	/**
	 * 根据文件名模糊查找 
	 */
	@Override
	public List findFileByName(String filesName , int type) {
		String hql = null;
		Query query = null;
		if(type == 0){
			hql = "from Files f where f.fileName like '%"+filesName+"%'";
			query = this.getSession().createQuery(hql);
		}else{
			hql = "from Files f where f.fileName like '%"+filesName+"%' and f.type=?";
			query = this.getSession().createQuery(hql);
			query.setParameter(0, type);
		}
		
		return query.list();
	}
	
	/**
	 * 获取文件getFiles：按照上传最新时间排序，参数page，size实现分页功能
	 */
	@Override
	public PageVO getFiles(int page,int size) {
		String hql = "from Files f order by f.uploadDate desc";
		Query query = this.getSession().createQuery(hql);
		//得到滚动集
		ScrollableResults scroll = query.scroll();
		//滚动到最后一行
		scroll.last();
		//获得总行数
		int count = scroll.getRowNumber() + 1 ;
		//设置分页信息
		query.setFirstResult(page - 1);
		query.setMaxResults(size);
		//打包入pageVO
		PageVO pageMessage = new PageVO();
		pageMessage.setCountPage((count/size) + 1);
		pageMessage.setRecordsNum(count);
		pageMessage.setList(query.list());
		return pageMessage;
	}
	
	/**
	 * 根据type来获取图片
	 */
	@Override
	public List getFilesByType(int type) {
		String hql = "from Files f where f.type=?";
		Query query = this.getSession().createQuery(hql);
		query.setParameter(0, type);
		return query.list();
	}

	@Override
	public void delFiles(Files files) {
		// TODO Auto-generated method stub
		this.getSession().delete(files);
	}
	//根据类型来获取文件（0是全部）
	@Override
	public PageVO getFiles(int page, int size, int type) {
		String hql = null;
		Query query = null;
		if(type==0){
			hql = "from Files f order by f.uploadDate desc";
			query = this.getSession().createQuery(hql);
		}else{
			hql = "from Files f where f.type=? order by f.uploadDate desc";
			query = this.getSession().createQuery(hql);
			query.setParameter(0, type);
		}
		
		//得到滚动集
		ScrollableResults scroll = query.scroll();
		//滚动到最后一行
		scroll.last();
		//获得总行数
		int count = scroll.getRowNumber() + 1 ;
		//设置分页信息
		query.setFirstResult(page - 1);
		query.setMaxResults(size);
		//打包入pageVO
		PageVO pageMessage = new PageVO();
		pageMessage.setCountPage((count/size) + 1);
		pageMessage.setRecordsNum(count);
		pageMessage.setList(query.list());
		return pageMessage;
	}

	@Override
	public List findFileByName(String filesName) {
		// TODO Auto-generated method stub
		return null;
	}
}
